<?php


/**
 * Class with action methods.
 */
class Web_Action_Methods{
	
	/**
	 * Reference to controller object
	 * @var Psa_Plugin_Controller
	 */
	private $controller = null;


	/**
	 * Constructor.
	 * Sets reference to controller object
	 */
	function __construct(&$controller){

		$this->controller = $controller;
	}


	/**
	 * This is default action.
	 * This action will be called if no other action is requested.
	 */
	function index(){

		// Check authorization. Every action that needs authorized user should call this method first.
		$this->controller->check_login();
		
		// Set which plugins to run.
		$run_plugins['view']['Main_View']['index'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		
		// Call plugins.
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Login action.
	 */
	function login(){

		$run_plugins['view']['Main_View']['login'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array('only_content');
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Logout.
	 */
	function logout(){

		// kill session and delete cookie
		$_SESSION = array();
		if(isset($_COOKIE[session_name()])){
			setcookie(session_name(), '', time() - 42000, '/');
		}
		session_destroy();
		
		// show login form
		$this->login();
	}


	/**
	 * Action for even numbers.
	 */
	function even_numbers(){

		$this->controller->check_login();
		$run_plugins['model']['Number_Operations']['even_numbers'] = array(100);
		$run_plugins['view']['Numbers_View']['even_numbers'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Action for odd numbers.
	 */
	function odd_numbers(){

		$this->controller->check_login();
		$run_plugins['model']['Number_Operations']['odd_numbers'] = array();
		$run_plugins['view']['Numbers_View']['odd_numbers'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Settings page.
	 */
	function settings(){

		$this->controller->check_login();
		$run_plugins['model']['User_Settings']['get_settings'] = array();
		$run_plugins['view']['Settings_View']['show_settings'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Save settings action.
	 */
	function settings_save(){

		$this->controller->check_login();
		$run_plugins['model']['User_Settings']['save_settings'] = array($_POST);
		$run_plugins['model']['User_Settings']['get_settings'] = array();
		$run_plugins['view']['Settings_View']['show_settings'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Search Flickr.
	 */
	function picture_serch(){

		$this->controller->check_login();
		$run_plugins['model']['Flickr']['search_photos'] = array($_POST['picture_serch']);
		$run_plugins['view']['Flickr_View']['images'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Action for prim numbers.
	 */
	function prim_numbers(){

		$this->controller->check_login();
		$run_plugins['model']['Number_Operations']['prim_numbers'] = array((int) @$_GET['max_prim']);
		$run_plugins['view']['Numbers_View']['prim_numbers'] = array();
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Action if Psa_Validation_Exception is raised.
	 */
	function validation_exeption($url_args){

		$this->controller->check_login();
		$run_plugins['view']['Main_View']['validation_exception'] = array($url_args[1]);
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}
	
	
	/**
	 * Action if Unauthorized_Access_Exception is raised.
	 */
	function unpermitted_action_exception($url_args){

		$this->controller->check_login();
		$run_plugins['view']['Main_View']['unpermitted_action_exception'] = array($url_args[1]);
		$run_plugins['view']['Main_View']['generate_html'] = array();
		$this->controller->run_plugins($run_plugins);
	}


	/**
	 * Unauthorized action example
	 */
	function no_access($url_args){

		$this->controller->check_login();
		$run_plugins['model']['App2']['unpermitted_action'] = array();
		$this->controller->run_plugins($run_plugins);
	}
}

?>